package edu.bbu.license.client.android.gui.files.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import edu.bbu.license.client.android.R;
import edu.bbu.license.client.android.gui.files.model.FileViewHolder;
import edu.bbu.license.client.android.services.file.model.FileModel;

public class FilesActivityFilesGridAdapter extends BaseAdapter {

	private List<FileModel> files;
	
	public FilesActivityFilesGridAdapter(List<FileModel> files) {
		this.files = files;
	}
	
	@Override
	public int getCount() {
		return files.size();
	}

	@Override
	public Object getItem(int position) {
		return files.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View gridItemView;
		
		if(convertView == null) {
			LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			gridItemView = inflater.inflate(R.layout.files_activity_file_item, null);

			TextView gridItemText = (TextView) gridItemView.findViewById(R.id.files_activity_file_item_text);
			ImageView gridItemIcon = (ImageView) gridItemView.findViewById(R.id.files_activity_file_item_icon);
			
			FileViewHolder fileViewHolder = new FileViewHolder(gridItemIcon, gridItemText);
			gridItemView.setTag(fileViewHolder);			
		} else {
			gridItemView = convertView;
		}
		
		FileViewHolder fileViewHolder = (FileViewHolder) gridItemView.getTag();
		fileViewHolder.getTextView().setText(files.get(position).getName());
		
		if(files.get(position).isFile()) {
			fileViewHolder.getImageView().setImageResource(R.drawable.files_content_icon_file);
		} else if(files.get(position).isDirectory()) {
			fileViewHolder.getImageView().setImageResource(R.drawable.files_content_icon_folder);
		} else {
			fileViewHolder.getImageView().setImageResource(R.drawable.files_content_icon_back);
		}
		
		return gridItemView;
	}
}
